﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageRoomTypesController : BaseController
    {
        public IRoomTypesRepository RoomTypesRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageRoomTypesController(IRoomTypesRepository roomTypesRepository, ILanguagesRepository languagesRepository)
        {
            RoomTypesRepository = roomTypesRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /RoomTypes/

        public ViewResult Index()
        {
            var roomTypes = RoomTypesRepository.FindAllByLang(Language);
            return View(roomTypes);
        }

        //
        // GET: /RoomTypes/Details/5

        public ViewResult Details(Int32 id)
        {
            var roomType = RoomTypesRepository.GetById(id);
            return View(roomType);
        }

        //
        // GET: /RoomTypes/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /RoomTypes/Create

        [HttpPost]
        public ActionResult Create(RoomType roomType)
        {
            if (ModelState.IsValid)
            {
                roomType.LanguageId = Language.Id;
                RoomTypesRepository.Insert(roomType);
                RoomTypesRepository.Save();
                return RedirectToAction("Index");
            }

            return View(roomType);
        }

        //
        // GET: /RoomTypes/Edit/5

        public ActionResult Edit(int id)
        {
            var roomType = RoomTypesRepository.GetById(id);
            return View(roomType);
        }

        //
        // POST: /RoomTypes/Edit/5

        [HttpPost]
        public ActionResult Edit(RoomType roomType)
        {
            var dbRoomType = RoomTypesRepository.GetById(roomType.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbRoomType);
                RoomTypesRepository.Save();
                return RedirectToAction("Index");
            }
            return View(roomType);
        }

        //
        // GET: /RoomTypes/Delete/5

        public ActionResult Delete(int id)
        {
            var roomType = RoomTypesRepository.GetById(id);
            return View(roomType);
        }

        //
        // POST: /RoomTypes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var roomType = RoomTypesRepository.GetById(id);
            RoomTypesRepository.Delete(roomType);
            RoomTypesRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            RoomTypesRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}